<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/icon.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/yzr_style.css"/>">

<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
<title>报损</title>
</head>
<body>
	<script type="text/javascript">
	$(function(){
		//打开选择药品窗口
		$("#lmb-hcbs-btn").linkbutton({onClick: function(){
			
			var lmb_ck = $("#lmb-baosun-store").combogrid("getValue");
			if(lmb_ck == ''){
				$.messager.alert("提示", "请选择盘点仓库！");
			}else{
				$("#lmb-selectBs-panel").window("open");
				$("#tb-ck-bs_dg").datagrid({
		    		url: 'to_HcByCkId', 
		    		queryParams: {
		    			ckId: lmb_ck
		    		},
		    		method: 'get'
		    	});
			}
			
			
		}});
		
		//显示全部
		$("#lmb-bs-reload").linkbutton({onClick:function(){
			$("#tb-ck-bs_dg").datagrid({
	    		url: 'to_findAllHcByCkId', 
	    		method: 'get'
	    	});			
		}});
			
		//搜索
		$('#lmb-bs-search').searchbox({
		    searcher:function(value,name){
		    	if(value == ''){
		    		$.messager.alert("提示", "请输入查询条件！");
		    	}else{
		    		$("#tb-ck-bs_dg").datagrid({	    		
			    		url: 'to_findAllHcByCname',
			    		method: 'post', 
			    		queryParams: {
			    			page:1,
			    			rows:13,
			    			cName: value
			    	}});
		    	}		    	
		    }
		}); 
		
		//耗材编号，耗材名，批号，单位，库存数量，结存数量，盈亏数量 
		var lmb_bs_cName = undefined;//耗材名
		var lmb_bs_BatchNum = undefined;//批号
		var lmb_bs_storeNum = undefined;//库存数量
		
		var lmb_bs_cid = undefined;//耗材id
		var lmb_bs_norms = undefined;//单位
		

		$("#lmb-ok-bs-selectHc").linkbutton({onClick: function(){
			var rows = $("#tb-ck-bs_dg").datagrid("getSelections");
			console.log(rows);
			$("#lmb_hcbs_form").form("validate");
			if(rows.length == 1){
				lmb_bs_cid = rows[0].cstocCid;
				lmb_bs_cName = rows[0].cstocCname;
				lmb_bs_BatchNum = rows[0].cstocBatchNum;
				lmb_bs_norms = rows[0].cstocNorms;
				lmb_bs_storeNum = rows[0].cstocNum;
				$("#lmb-baosun-cname").textbox("setValue",lmb_bs_cName);
				$("#lmb-baosun-pihao").textbox("setValue",lmb_bs_BatchNum);
				$("#lmb-baosun-kcnumber").textbox("setValue",lmb_bs_storeNum);
				$("#lmb-baosun-haocaiId").textbox("setValue",lmb_bs_cid);
				$("#lmb-baosun-haocaiNorms").textbox("setValue",lmb_bs_norms);
				$("#lmb-selectBs-panel").window("close");
				
				
				lmb_bs_cName = undefined;
				lmb_bs_BatchNum = undefined;
				lmb_bs_storeNum = undefined;
				
				lmb_bs_cid = undefined;
				lmb_bs_norms = undefined;	 
			
			}else{
				$.messager.alert("提示", "您还没有选择记录！");
			}
		}});
				
		//添加数据到datagrid
		$("#lmb-baosun-btn-add").linkbutton({onClick: function(){
			//<!-- mdtCid, mdtCname,mdtBatchNum,mdtNorms,mdtKcnum,mdtNum-->
			//验证表单是否完善
			if($('#lmb_hcbs_form').form("validate")){
				//库存数量
				var lmb_num = $("#lmb-baosun-kcnumber").textbox("getValue");		
				//报损数量
				var lmb_bs_num = $("#lmb-baosun-bssl").numberbox("getValue");
				if(lmb_num == ''){
					alert("请完善所有数据！");
				}else{
					if(eval(lmb_bs_num) < eval(lmb_num)){
						$('#lmb-hcbs-dg').datagrid('appendRow',{
							mdtCid: $("#lmb-baosun-haocaiId").textbox("getValue"),
							mdtCname: $("#lmb-baosun-cname").textbox("getValue"),
							mdtBatchNum: $("#lmb-baosun-pihao").textbox("getValue"),
							mdtNorms: $("#lmb-baosun-haocaiNorms").textbox("getValue"),
							mdtKcnum:$("#lmb-baosun-kcnumber").textbox("getValue"),
							mdtNum: $("#lmb-baosun-bssl").numberbox("getValue"),
						});
						
						//清除部分数据
						$("#lmb-baosun-haocaiId").textbox("clear"),
						$("#lmb-baosun-cname").textbox("clear"),
						$("#lmb-baosun-pihao").textbox("clear"),
						$("#lmb-baosun-haocaiNorms").textbox("clear"),
						$("#lmb-baosun-kcnumber").textbox("clear"),
						$("#lmb-baosun-bssl").numberbox("clear")
					}else{
						$.messager.alert("提示", "报损数量大于库存数量！");
					}	
				}				
			}else{
				alert("请完善所有数据！");
			}		
		}});	
		
		//删除行
		$("#lmb-baosunDet-btn—cancel").linkbutton({onClick: function(){
			var rows = $("#lmb-hcbs-dg").datagrid("getSelections");			
			var lmb_bs_index =$('#lmb-hcbs-dg').datagrid('getRowIndex', rows[0]);
			$('#lmb-hcbs-dg').datagrid('deleteRow',lmb_bs_index);
		}});
		
		
		
		
		//提交数据到数据库//lmb-pandianDet-tijiao
		$("#lmb-baosunDet-tijiao").linkbutton({onClick: function(){
				
			var rows = $("#lmb-hcbs-dg").datagrid("getRows");
			console.log(JSON.stringify(rows));

			//耗材编号，耗材名称，批号，单位，库存数量，报损数量
			for(var i = 0; i < rows.length; i ++){
				generateHiddenLmb_baosun(rows[i], "mdtCid", i);
				generateHiddenLmb_baosun(rows[i], "mdtCname", i);
				generateHiddenLmb_baosun(rows[i], "mdtBatchNum", i);
				generateHiddenLmb_baosun(rows[i], "mdtNorms", i);
				generateHiddenLmb_baosun(rows[i], "mdtNum", i);
			}
					
			$('#lmb_hcbs_form').form('submit', {				
				url: 'toLmbHcBaoSun', 
				onSubmit: function(){
					var jg=$('#lmb_hcbs_form').form("validate");
			    	if(jg==false){
			    		$.messager.alert("提示", "请完善所有数据！");
			    		return false;
			    	}
			    }, 
				success: function(v){
					$.messager.alert('提示', "提交成功");
					$(":hidden[x='y']").remove();			
					$('#lmb_hcbs_form').form("clear");
					$('#lmb-hcbs-dg').datagrid('loadData', { total: 0, rows: [] }); 
				}
			});
		}});

		//创建隐式表单域来保存明细
		function generateHiddenLmb_baosun(row, name, i){
			var lmb_hcbs_form = $('#lmb_hcbs_form');
			//type='hidden'
			$("<input type='hidden' x='y'>")
				.attr("name", "damageDetail[" + i + ']' + '.' + name)
				.val(row[name])
				.appendTo(lmb_hcbs_form);
		}	
	});
	
</script> 
<!-- style="display:none;" -->

		<div style="display:none;">
			<input id="lmb-baosun-haocaiId" class="easyui-textbox" 
			data-options="labelAlign:'right',label:'耗材编号'"/>
			
			<input id="lmb-baosun-haocaiNorms" class="easyui-textbox" 
			data-options="labelAlign:'right',label:'单位'"/>
		</div>
	
	<form id="lmb_hcbs_form" method="post" style="margin-top:0px;">
		<table width="100%" style="background: rgb(236,243,255); padding: 10px; border-radius: 5px;">
			
			<tr>
				<td style="width:50%;">
					<input id="lmb-baosun-bsrq"class="easyui-datebox" name="damage.mdDate"
						data-options="label:'报损日期',labelAlign:'right',width:'60%',required:true">
				</td>				
				<td style="width:50%;">
					<select id="lmb-baosun-staff" name="damage.mdCgy" class="easyui-combogrid"
					data-options="
							label: '仓管员',
							labelAlign:'right',
							required:true,
							width: '60%',
							panelWidth: '290',
							panelHeight: 'auto',
							url: 'queryAllStaff',
							pagination: true,
							pageSize: 8,
							pageList: [8, 16, 24, 32],
							idField: 'staffNo',
							textField: 'staffName',
							columns: [[
								{field:'staffName',title:'姓名',width:100},
								{field:'departmentName',title:'部门',width:100},
								{field:'postName',title:'职务',width:85}
							]]
					"></select>
				</td>			
			</tr>
			<tr>
				<td style="width:50%;">
					<select id="lmb-baosun-store"  name="damage.mdCk" class="easyui-combogrid"
					data-options="
						label: '仓库',
						labelAlign:'right',
						required:true,
						width: '60%',
						panelWidth: '290',
						panelHeight: 'auto',				
						url: 'queryAllStorehouse',
						pagination: true,
						pageSize: 8,
						pageList: [8, 16, 24, 32],
						idField: 'storeId',
						textField: 'storeName',
						columns: [[
							{field:'storeName',title:'仓库名',width:100},
							{field:'storeKind',title:'类型',width:80},
							{field:'storeAddress',title:'地址 ',width:100}
						]]
					"></select>
				</td>
				
				<td style="width:50%;">
					<input  id="lmb-baosun-cname" class="easyui-textbox"  data-options="width:'60%',labelAlign:'right',label:'耗材',readonly:'readonly'">	
					<a id="lmb-hcbs-btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">查找</a>				
				</td>									
			</tr>
			<tr>
				<td style="width:50%;">
					<input  id="lmb-baosun-pihao" class="easyui-textbox"  data-options="width:'60%',labelAlign:'right',label:'批号',readonly:'readonly'">					
				</td>	
				<td style="width:50%;">
					<input id="lmb-baosun-kcnumber" class="easyui-textbox"  data-options="width:'60%',labelAlign:'right',label:'库存数量',readonly:'readonly'">	
				</td>			
			</tr>
			<tr>
				<td style="width:50%;">
					<input id="lmb-baosun-bssm" name="damage.mdRemark" class="easyui-textbox"  data-options="width:'60%',height:'50px',multiline:true,labelAlign:'right',label:'损溢说明'">	
				</td>
				<td style="width:50%;">
					<input id="lmb-baosun-bssl" class="easyui-numberbox"  data-options="width:'60%',labelAlign:'right',label:'报损数量'">	
					<a id="lmb-baosun-btn-add" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加</a>
				</td>						
			</tr>
			
		</table>
		
		<!-- 订单明细 -->
		<div style="margin: 5px;">
			<div id="lmb-hcpd-tabs" class="easyui-tabs" style="width:100%; margin-top:20px;" 
				data-options="plain:true,narrow:true">
				
				<div title="报损耗材" style="padding:10px;">
					<table id="lmb-hcbs-dg" class="easyui-datagrid" 
							data-options="
								iconCls: 'icon-edit',									
								width: '100%',
								toolbar: '#lmb_bstj_tb1',
								pagination: true,						
								pageSize: 8,
								pageList: [8, 16, 24, 32],
								">
							<thead>
								<tr>	
									<th data-options="field:'mdtCid',width:'20%',align:'center'">耗材编号</th>			
									<th data-options="field:'mdtCname',width:'18%',align:'center'">耗材名称</th>
									<th data-options="field:'mdtBatchNum',width:'14%',align:'center'">批号</th>
									<th data-options="field:'mdtNorms',width:'12%',align:'center'">单位</th>
									<th data-options="field:'mdtKcnum',width:'12%',align:'center'">库存数量</th>
									<th data-options="field:'mdtNum',width:'12%',align:'center'">报损数量</th>
								</tr>
							</thead>
					 </table>
					 <div id="lmb_bstj_tb1" style="height:auto">
						<a id="lmb-baosunDet-tijiao" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-ok'">提交</a>
						<a id="lmb-baosunDet-btn—cancel" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">删除一行</a>			
					</div>
				</div>							
			</div>						
		</div>
	</form>
	
	<div id="lmb-selectBs-panel" class="easyui-window" title="选择盘点耗材" style="width:850px;height:350px;" data-options="closable:true,closed:true,collapsible:false,minimizable:false,maximizable:false">
		<table id="tb-ck-bs_dg" class="easyui-datagrid" 
			data-options="			
				fit:true,
				pagination: true,
				pageSize: 8,
				pageList: [8, 16, 24, 32],
				rownumbers: false,
				toolbar: '#lmb-toolbar-baosun',
				border:false,
				singleSelect:true
			">
			<thead>
				<tr>				
					<th data-options="field:'ckName',width:'20%',align:'center',editor:'textbox'">仓库</th>				
					<th data-options="field:'cstocCid',width:'20%',align:'center',editor:'textbox'">耗材编号</th>				
					<th data-options="field:'cstocCname',width:'20%',align:'center'">耗材名称</th>
					<th data-options="field:'cstocBatchNum',width:'13%',align:'center'">批号</th>					
					<th data-options="field:'cstocNorms',width:'13%',align:'center'">单位</th>	
					<th data-options="field:'cstocNum',width:'13%',align:'center'">数量</th>																								
				</tr>
			</thead>
		</table>				    
	    
	    <!-- 工具栏 -->
	    <div id="lmb-toolbar-baosun">
	    	<input id="lmb-bs-search" class="easyui-searchbox" data-options="menu: '#lmb-baosun—serch', width: 250, prompt: '请输入关键字'"/>    	
	    	<a id="lmb-bs-reload" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true">刷新</a>
	    	<a id="lmb-ok-bs-selectHc" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">确定</a>
	    </div>
	    	    
	    <!-- 搜索框 -->
	    <div id="lmb-baosun—serch" style="width:120px">
	    	<div data-options="name:'cName'">耗材名称</div>
		</div>		
	</div>			
</body>
</html>